package visual;


import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;

import data.MatrixTime;
import utils.kml.*;



public class KMLGraph {
	
	
	public static void printMap(List<MatrixTime> erlang_times, String out_file) {	
		
		try {
			
			String kml_file = new String(out_file);
			if(out_file.endsWith(".kmz"))
				kml_file = out_file.substring(0,out_file.indexOf(".kmz"))+".kml";
			
			Kml utilKml = new Kml();
			
			PrintWriter out = new PrintWriter(new FileWriter(new File(kml_file)));
			
			utilKml.printHeaderFolder(out, "Trend "+out_file.substring(out_file.indexOf("/")+1));
			
			MatrixTime et = erlang_times.get(0);
			printMap(et,erlang_times,out);
			
			utilKml.printMrTyp(out);
			utilKml.printFooterFolder(out);			

			out.close();
			
			if(out_file.endsWith(".kmz"))
				KML2KMZ.run(out_file);
			
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void printMap(List<MatrixTime> erlang_times, List<MatrixTime> mean_times, String out_file) {	
		
		try {
			
			String kml_file = new String(out_file);
			if(out_file.endsWith(".kmz"))
				kml_file = out_file.substring(0,out_file.indexOf(".kmz"))+".kml";
			
			Kml utilKml = new Kml();
			
			PrintWriter out = new PrintWriter(new FileWriter(new File(kml_file)));
			
			utilKml.printHeaderFolder(out, "Trend "+out_file.substring(out_file.indexOf("/")+1));
			
			MatrixTime et = erlang_times.get(0);
			printMap(et,erlang_times,mean_times,out);
			
			utilKml.printMrTyp(out);
			utilKml.printFooterFolder(out);			

			out.close();
			
			if(out_file.endsWith(".kmz"))
				KML2KMZ.run(out_file);
			
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static final SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
	
	private static void printMap(MatrixTime mt, List<MatrixTime> erlang_times, PrintWriter out) throws Exception {	
			
		out.println("<Folder>");
		out.println("<name>"+FORMATTER.format(mt.getStart().getTime())+"-"+FORMATTER.format(mt.getEnd().getTime())+"</name>");
		out.println("<drawOrder>1</drawOrder>");
		KMLSquare kml_square = new KMLSquare();
		
		for(int i=0; i<mt.getMat().length; i++)
		for(int j=0; j<mt.getMat()[i].length;j++) {
				String description = GraphPlotter.plot(erlang_times,i,j);
				out.println(printSquare(mt, i, j, description,kml_square));
		}
		out.println("</Folder>");
	}
	
	private static void printMap(MatrixTime mt, List<MatrixTime> erlang_times, List<MatrixTime> mean_times, PrintWriter out) throws Exception {	
		
		out.println("<Folder>");
		out.println("<name>"+FORMATTER.format(mt.getStart().getTime())+"-"+FORMATTER.format(mt.getEnd().getTime())+"</name>");
		out.println("<drawOrder>1</drawOrder>");
		KMLSquare kml_square = new KMLSquare();
		
		for(int i=0; i<mt.getMat().length; i++)
		for(int j=0; j<mt.getMat()[i].length;j++) {
				String description = GraphPlotter.plot(erlang_times,mean_times,i,j);
				out.println(printSquare(mt, i, j, description,kml_square));
		}
		out.println("</Folder>");
	}
	
	private static String printSquare(MatrixTime mt, int i, int j, String description, KMLSquare kml_square) {
		double[][] ll = mt.getArea().getCellBorder(i, j);
		String name = "("+i+","+j+")";
		return kml_square.draw(ll, name, "01010000", "ffffffff", description);
	}
}
